package com.tianzmp.ttq.admin.controller;

import com.tianzmp.ttq.admin.common.BaseResult;
import com.tianzmp.ttq.admin.common.Result;
import com.tianzmp.ttq.admin.controller.reqDto.MenuAddReqDto;
import com.tianzmp.ttq.admin.controller.reqDto.MenuPageListReqDto;
import com.tianzmp.ttq.admin.controller.reqDto.MenuUpdateReqDto;
import com.tianzmp.ttq.admin.controller.resDto.MenuListResDto;
import com.tianzmp.ttq.admin.controller.resDto.MenuTreeResDto;
import com.tianzmp.ttq.admin.entity.Menu;
import com.tianzmp.ttq.admin.entity.PageInfo;
import com.tianzmp.ttq.admin.service.IMenuService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 菜单 前端控制器
 * </p>
 *
 * @author ttq
 * @since 2022-07-30
 */
@RestController
@RequestMapping("/admin/menu")
public class MenuController {

    @Resource
    private IMenuService menuService;


    /**
     * <pre>
     *     this is a user list
     * </pre>
     * @return
     */
    @ApiOperation(value = "不分页查菜单",notes = "不分页查询所有菜单",httpMethod = "GET")
    @GetMapping("/list")
    public BaseResult<List<MenuListResDto>> list(){
        List<MenuListResDto> list = menuService.list();
        return Result.ok(list);
    }

    /**
     * <pre>
     *     user list is a pageInfo
     * </pre>
     * @param dto  当前第几页
     * @return
     */
    @ApiOperation(value = "分页查询菜单",notes = "分页查询菜单",httpMethod = "POST")
    @PostMapping(value = "/pageList")
    public BaseResult<PageInfo<List<MenuTreeResDto>>> pageList(@RequestBody MenuPageListReqDto dto){
        PageInfo<List<MenuTreeResDto>> listPageInfo = menuService.pageList(dto);
        return Result.ok(listPageInfo);
    }


    /**
     * <pre>
     *
     * </pre>
     * @param menuId role-id
     * @return
     */
    @ApiOperation(value = "查询菜单详情",notes = "根据menuId查询菜单详情",httpMethod = "GET")
    @GetMapping(value = "/getById/{menuId}")
    public BaseResult<Menu> getById(@PathVariable(value = "menuId",required = true)Long menuId){
        Menu bean = menuService.getById(menuId);
        return Result.ok(bean);
    }

    @ApiOperation(value = "更新菜单",notes = "根据menuId更新",httpMethod = "POST")
    @PostMapping(value = "/update")
    public BaseResult<Boolean> update(@Valid @RequestBody MenuUpdateReqDto dto){
        boolean result = menuService.update(dto);
        return Result.ok(result);
    }


    @ApiOperation(value = "新增菜单",notes = "新增菜单",httpMethod = "POST")
    @PostMapping(value = "/add")
    public BaseResult<Boolean> save(@Valid @RequestBody MenuAddReqDto dto){
        boolean result = menuService.insert(dto);
        return Result.ok(result);
    }

    @ApiOperation(value = "删除菜单",notes = "根据角色菜单",httpMethod = "DELETE")
    @DeleteMapping(value = "/delete/{menuId}")
    public BaseResult<Boolean> delete(@PathVariable(value = "menuId",required = true) Long menuId){
        boolean result = menuService.delete(menuId);
        return Result.ok(result);
    }




}
